package com.game.Components;

import com.Math.Polygon2D;
import com.Math.Vector2D;
import com.pixels.graphics.Drawer;

public class Asteroid extends Entity {
	
	private Vector2D[] a;
	public double radius;
	int MIN = 10;
	int MAX = 20;
	int maxSpeed = 20;
	public int level;

	public Asteroid(int level) {
		super();

		this.level = level;
		
		setLevel(level);
		
		pos = new Vector2D((Math.random() * width), (Math.random() * height));
		while (pos.x < 500 && pos.x > 300 && pos.y < 400 && pos.y > 200) {
			pos = new Vector2D((Math.random() * width), (Math.random() * height));
		}
		makeAstroid();

		polygon.moveTo(pos);
	}

	public void setLevel(int level) {
		if (level > 0) {
			this.level = level;
			MAX = 10*level;
			MIN = 20*level;
			maxSpeed = 20/level;
			makeAstroid();
		}
	}

	public void destroy() {

	}

	public void draw(Drawer draw) {
		draw.setColor(0xffffff);
		draw.drawPoly(polygon);
	}

	public void downGrade() {
		setLevel(level-1);
		if (level < 1) {
			destroy();
		}
	}

	public void makeAstroid() {
		int s = MIN + (int) (Math.random() * (MAX - MIN));

		a = new Vector2D[s];

		for (int j = 0; j < s; j++) {
			double dir = 2 * Math.PI / s * j;
			double r = MIN + (Math.random() * (MAX - MIN));
			radius += r;
			double x = (int) -Math.round(r * Math.sin(dir));
			double y = (int) Math.round(r * Math.cos(dir));

			a[j] = new Vector2D(x, y);
		}
		radius /= s;

		speed = new Vector2D(Math.random() * maxSpeed - (maxSpeed / 2), Math.random() * maxSpeed - (maxSpeed / 2));
		rotateSpeed = Math.PI/(Math.round(Math.random()*360)+1);
		polygon = new Polygon2D(a);
		
	}

	public void move() {
		pos.add(speed);
		polygon.moveTo(pos);
		boundaries();
		polygon.rotateRad(rotateSpeed);
	}

}
